package com.ys.nms.module.business.service.customOid;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ys.nms.module.business.controller.admin.customOid.vo.*;
import com.ys.nms.module.business.dal.dataobject.customOid.CustomOidDO;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.util.object.BeanUtils;

import com.ys.nms.module.business.dal.mysql.customOid.CustomOidMapper;

import static com.ys.nms.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.convertList;
import static com.ys.nms.framework.common.util.collection.CollectionUtils.diffList;
import static com.ys.nms.module.business.enums.ErrorCodeConstants.*;

/**
 * 自定义OID Service 实现类
 *
 * @author ZZY
 */
@Service
@Validated
public class CustomOidServiceImpl implements CustomOidService {

    @Resource
    private CustomOidMapper customOidMapper;

    @Override
    public Integer createCustomOid(CustomOidSaveReqVO createReqVO) {
        // 插入
        CustomOidDO customOid = BeanUtils.toBean(createReqVO, CustomOidDO.class);
        customOidMapper.insert(customOid);

        // 返回
        return customOid.getCustomoidId();
    }

    @Override
    public void updateCustomOid(CustomOidSaveReqVO updateReqVO) {
        // 校验存在
        validateCustomOidExists(updateReqVO.getCustomoidId());
        // 更新
        CustomOidDO updateObj = BeanUtils.toBean(updateReqVO, CustomOidDO.class);
        customOidMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomOid(Integer id) {
        // 校验存在
        validateCustomOidExists(id);
        // 删除
        customOidMapper.deleteById(id);
    }

    @Override
        public void deleteCustomOidListByIds(List<Integer> ids) {
        // 删除
        customOidMapper.deleteByIds(ids);
        }


    private void validateCustomOidExists(Integer id) {
        if (customOidMapper.selectById(id) == null) {
            throw exception(CUSTOM_OID_NOT_EXISTS);
        }
    }

    @Override
    public CustomOidDO getCustomOid(Integer id) {
        return customOidMapper.selectById(id);
    }

    @Override
    public PageResult<CustomOidDO> getCustomOidPage(CustomOidPageReqVO pageReqVO) {
        return customOidMapper.selectPage(pageReqVO);
    }

}